package function;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @version 1.0
 * @program: math test
 * @description: 连接txt
 * @author: Kishan Liu
 * @create: 2021-12-10 16:05
 **/
public class DataBase {
    public static void writeData(String id, int score, int time, String evaluation) throws IOException {
        FileOutputStream fw=new FileOutputStream("data.txt",true);//ture表示追加写入。如果不需要追加写入就直接去掉这个参数就行
        OutputStreamWriter osw=new OutputStreamWriter(fw, StandardCharsets.UTF_8);
        BufferedWriter bw=new BufferedWriter(osw);
        bw.write("用户："+id + "  得分："+score + "  用时："+time + "秒  评级："+evaluation+"\r\n");
        bw.close();
        fw.close();
    }


    public static void readData() throws IOException {
        FileReader in=new FileReader("data.txt");
        BufferedReader reader=new BufferedReader(in);
        String m;
        while((m=reader.readLine())!=null){
            System.out.println(m);
        }
    }

    //读取最后一行
    public static void readLastData() throws FileNotFoundException {
        Scanner sc = new Scanner(new FileReader("data.txt"));
        String line;
        while ((sc.hasNextLine() && (line = sc.nextLine()) != null)) {
            if (!sc.hasNextLine())
                System.out.println(line);
        }
        sc.close();
    }

}


